package com.cerosoft.components;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.cerosoft.utils.EventManager.EventType;
import com.cerosoft.utils.IndexChangeListener;

public class PushButton extends ImageView implements IndexChangeListener {

	private int max = -1;

	private int currIndex = -1;

	private EventType type = null;

	public void initBtn(EventType type, int currIndex, int max) {
		this.type = type;
		this.currIndex = currIndex;
		this.max = max;
		if (type != null) {
			switch (type) {
			case Prev: {
				if (currIndex == 0) {
					setEnabled(false);
				}
				break;
			}
			case Next: {
				if (currIndex == max - 1) {
					setEnabled(false);
				}
				break;
			}
			}
		}
	}

	public void setMaxIndex(int max) {
	}

	public PushButton(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public int indexAdd() {
		if (type != null && max != -1) {
			switch (type) {
			case Prev: {
				if (currIndex < max - 1) {
					currIndex++;
				}
				setEnabled(true);
				break;
			}
			case Next: {
				if (currIndex == max - 1) {
					setEnabled(false);
				} else if (currIndex == max - 2) {
					currIndex++;
					setEnabled(false);
				} else {
					currIndex++;
					setEnabled(true);
				}
				break;
			}
			}
		}
		Log.d("index", "indexAdd c=" + currIndex);
		return currIndex;
	}

	@Override
	public int indexSub() {
		if (type != null && max != -1) {
			switch (type) {
			case Prev: {
				if (currIndex == 0) {
					setEnabled(false);
				} else if (currIndex == 1) {
					currIndex--;
					setEnabled(false);
				} else {
					currIndex--;
					setEnabled(true);
				}
				break;
			}
			case Next: {
				if (currIndex > 0) {
					currIndex--;
				}
				setEnabled(true);
				break;
			}
			}
		}
		Log.d("index", "indexSub c=" + currIndex);
		return currIndex;
	}

}
